package com.alan.base.config;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alan.base.App;
import com.alan.base.utils.FileUtils;
import com.alan.base.utils.Logger;
import com.alan.base.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class FileLogManager {

    private static final ExecutorService executor = Executors.newFixedThreadPool(1);

    private static FileLogManager manager;
    private final List<String> contentList = new ArrayList<>();

    private FileLogManager() {
    }

    /**
     * 单例
     *
     * @return 返沪 ConfigManager 实例
     */
    public synchronized static FileLogManager getInstance() {
        if (null == manager) {
            synchronized (FileLogManager.class) {
                if (null == manager) {
                    manager = new FileLogManager();
                }
            }
        }
        return manager;
    }

    private final Runnable runnable = this::executor;

    public void addContent(String string) {
        String s = Utils.getNow() + " [" + "thread name:" + Thread.currentThread().getName() + "] " + string;
        getContentList().add(s);
        if (contentList.size() >= 100) {
            executor();
        } else {
            App.getInstance().getMainHandler().removeCallbacks(runnable);
            App.getInstance().getMainHandler().postDelayed(runnable, 2000);
        }
    }

    public void write(String string) {
        String s = Utils.getNow() + "    " + string;
        getContentList().add(s);
        executor();
    }

    private synchronized List<String> getContentList() {
        return contentList;
    }

    public synchronized void executor() {
        List<String> list = new ArrayList<>(getContentList());
        getContentList().clear();
        executor.execute(() -> writeFile(list));
    }

    private synchronized void writeFile(List<String> list) {
        String fileName = ConfigManager.getInstance().getLogFileName();
        boolean isNeedWrite = false;
        if (TextUtils.isEmpty(fileName)) {
            fileName = "log" + System.currentTimeMillis() + ".log";
            isNeedWrite = true;
        }
        String path = FileUtils.getExtFilePath("cache");
        File file = new File(path, fileName);
        if (file.exists()) {
            long length = file.length();
            Logger.d("file length:" + length);
            if (length > 200 * 1024 * 1024) {
                fileName = "log" + System.currentTimeMillis() + ".log";
                isNeedWrite = true;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : list) {
            stringBuilder.append(s);
            stringBuilder.append("\n");
        }
        FileUtils.write(stringBuilder.toString(), path + "/" + fileName, true);
        if (isNeedWrite) {
            ConfigManager.getInstance().setLogFileName(fileName);
        }
    }
}
